package com.aiju.cash.ui.main.view.time;


import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.bigkoo.pickerview.TimePopupWindow;

import java.util.Date;


/**
 * Created by ZhiShui on 2015/6/30.
 * 通用上弹时间滚动选择器
 */
public class TimePopupHelper {
    private final static String TAG = "TimePopupHelper";
    TimePopupWindow mTimePopou;
    private helperInterface mInterface;
    private View parent;
    private Context mContext;
    private Date defaultDate;
    public static final int PICK_TIME_FROM = 1;
    public static final int PICK_TIME_END = 2;
    private int pickTimeTag = PICK_TIME_FROM;
    private boolean mIsShow = false;
    //时间滚动选择器类型，默认为年、月、日
    private TimePopupWindow.Type showType = TimePopupWindow.Type.MONTH_DAY_HOUR_MIN;

    public TimePopupHelper(Context context, View parent, Date defaultDate) {
        this.mContext = context;
        this.parent = parent;
        this.defaultDate = defaultDate;
        Log.e("TimePopupHelper", defaultDate.toString());
        Log.e("TimePopupHelper", this.defaultDate.toString());
        initPopou(context);
    }

    public TimePopupHelper(Context context, View parent, Date defaultDate, TimePopupWindow.Type type) {
        this.mContext = context;
        this.parent = parent;
        this.defaultDate = defaultDate;
        this.showType = type;
        Log.e("TimePopupHelper", defaultDate.toString());
        Log.e("TimePopupHelper", this.defaultDate.toString());
        initPopou(context);
    }

    public boolean isShow() {
        return mIsShow;
    }


    private void initPopou(Context context) {

        mTimePopou = new TimePopupWindow(context, showType);
        //时间选择后回调
        mTimePopou.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date, boolean isAm, String ampm) {
                if (mInterface != null) {
                    mInterface.callBackForData(pickTimeTag, date, isAm, ampm);
                    mIsShow = false;
                }
                mTimePopou.dismiss();
            }

            @Override
            public void candel() {
                if (mInterface != null) {
                    mInterface.cancel(pickTimeTag);
                    mIsShow = false;
                }
                mTimePopou.dismiss();
            }
        });
    }

    public void startPopou(int tag) {
        if (tag != PICK_TIME_FROM && tag != PICK_TIME_END) {
            Log.e(TAG, "tag init fail");
            return;
        }
        mIsShow = true;
        pickTimeTag = tag;
        if (mTimePopou == null)
            initPopou(mContext);
        mTimePopou.showAtLocation(parent, Gravity.BOTTOM, 0, 0, defaultDate);
    }


    public Date getDefaultDate() {
        return defaultDate;
    }

    public void setDefaultDate(Date defaultDate) {
        this.defaultDate = defaultDate;
    }

    public helperInterface getmInterface() {
        return mInterface;
    }

    public void setmInterface(helperInterface mInterface) {
        this.mInterface = mInterface;
    }

    public void close() {
        mIsShow = false;
        mTimePopou.dismiss();
    }

    public interface helperInterface{
        void cancel(int tag);
        void callBackForData(int tag, Date date, boolean isAm, String ampm);
    }

}
